package com.yonyou.brigade.cron.order.yihao;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.Developer;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IDeveloperService;
import com.yonyou.brigade.third_parties.yihao.service.ICLYHOrderService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import com.yonyou.ybzboot.starter.mail.model.Email;
import com.yonyou.ybzboot.starter.mail.service.IMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@ElasticJobConfig(cron = "0 30 2 * * ? ", shardingTotalCount = 1)
@Component
public class FetchYihaoOrders implements SimpleJob {

    Logger log = LoggerFactory.getLogger(this.getClass());


    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("developerServiceImpl")
    private IDeveloperService developerService;

    @Autowired
    @Qualifier("CLYHOrderServiceImpl")
    private ICLYHOrderService CLYHOrderService;

    @Autowired
    private IMailService mailService;

    @Value("${brigade.cur}")
    private String curEnv;

    @Override
    public void execute(ShardingContext shardingContext){
        log.info("<<<<clyh-pull-orders-begin>>>>");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.CLYH.getName());
        CopyOnWriteArrayList<String> channelConfigs = new CopyOnWriteArrayList<>();
        if (briChannels != null && briChannels.size() > 0) {
            for (int i = 0; i < briChannels.size(); i++) {
                BriChannel briChannel = briChannels.get(i);
                String channelConfig = briChannel.getConfig();
                if (channelConfigs.contains(channelConfig)) {
                    continue;
                }
                channelConfigs.add(channelConfig);
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                int channelId = briChannel.getId();
                String begin = DateUtils.getCurrentDateTimeMinusOneDay(DateUtils.DD_DATETIME_FORMAT);
                String end = DateUtils.getCurrentDateTime();
                try {
                    CLYHOrderService.getOrderList(tenant, orgpk, begin, end, briChannel);
                } catch (Exception e) {
                    Wrapper<Developer> developerWrapper = new EntityWrapper<>();
                    developerWrapper.where("type={0}", "KF");
                    List<Developer> developers = developerService.selectList(developerWrapper);
                    String developerEmails = "";
                    for (int j = 0; j < developers.size(); j++) {
                        developerEmails += developers.get(j).getEmail();
                        if (j < developers.size() - 1) {
                            developerEmails += ",";
                        }
                    }
                    Email email = new Email();
                    String[] emails = developerEmails.split(",");
                    email.setEmail(emails);
                    email.setSubject(curEnv + "环境差旅壹号拉单失败");
                    email.setContent(e.getMessage());
                    try {
                        log.error(e.getMessage());
                        mailService.send(email);
                    } catch (Exception e1) {
                        log.error("提示" + curEnv + "环境差旅壹号订单拉单失败的邮件发送失败！" + e1.getMessage());
                    }
                }

            }
        }
        log.info("<<<<clyh-pull-orders-end>>>>");
    }
}
